<HEAD> <TITLE> Classes </TITLE> </HEAD> <BODY BACKGROUND="http://www.imaginary.com/LPMud/Graphics/RecycledPaper.jpg" TEXT=#000000 ALINK=#FFFF00 VLINK=#550000 LINK=#000099> <center> <H1> Classes </H1> </center>
<p>
First of all, to clear up alot of confusion, don't read too much into the
name.  A class is a simple way of holding a collection of values in an
easily dealt with form.
<p>
classes are the only type which can be created; one defines a class as follows:
<p>
<pre>
  class myclass {
      type1 member1;
      type2 member2, ...;
      ...
  }
</pre>
<p>
where type1, type2, etc are type names, and 'myclass' is what you want to
refer to the class as.  'class myclass' then becomes a type, which is
legal wherever any other type is.  (Note:  The point of definition is the
name, so classes may actually contain instances of themselves).
<p>
Class definitions are copied by inheritance, and so should be defined in
base objects.  They can be defined in .h files, but this will likely
get you into trouble.
<p>
To create a variable of a class type, simply declare it as you would
any other variable:
<p>
<pre>
class myclass myvar;
</pre>
<p>
Note:  Like all other LPC variables, the value is initially zero.  Classes
share the <A HREF=http://www.imaginary.com/~beek/mudos/lpc/implicit_pointer.html>  implicit pointer </A> semantics of arrays and mappings, and are
freed automatically when nothing points to them any more.  A new class
instance ins created with the syntax new(class myclass).  For example,
if myvar had been defined as above, one could initialize it:
<p>
<pre>
myvar = new(class myclass);
</pre>
<p>
and as usual, the initialization could have been included as part of
the definition.  Members of the class are accessed with the -> operator.
One can use a member of a class anywhere one could use a normal variable.
For example, the syntax:
<p>
myvar->member1 = 1;
<p>
Initializes the member 'member1' of myvar to 1.
<p>
Here is a quick example:
<p>
<pre>
class event {
    int when;
    string what;
}
<p>
class event *history = ({ }); // declare an array of classes
<p>
void add(string what) {
    class this_event = new(class event); // create a new class
    this_event->when = time();
    this_event->what = what;
    history += ({ this_event });
}
<p>
class event get(int idx) {
    return history[idx];
}
</pre>
<p>
<HR> <ADDRESS> <A HREF=http://wagner.princeton.edu/~tim>  Tim Hollebeek </A> <p> <A HREF="http://www.imaginary.com/~beek/">  Beek </A>@ZorkMUD, Lima Bean, IdeaExchange, TMI-2, and elsewhere </ADDRESS>
